package com.mp.approvalreq.servlet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.Query;
import com.mp.approvalreq.entity.Request;
import com.mp.approvalreq.service.RequestServicePrx;
import com.mp.approvalreq.util.*;
import com.mp.user.entity.User;
import com.plusmoney.exception.RpcServerLogicalException;
import com.plusmoney.util.Utils;
import com.qq.tars.client.util.ServantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

;

/**
 * 获得任务关联的审批列表
 * <p>
 * 审批拉取关联的审批列表
 * <p>
 * CRM关联客户的视角的相关信息,先调德伟接口获取了关联信息(1审批,2任务),然后前端传审批id给这个接口
 */
@WebServlet("/getMissionRelatedRequestList")
public class GetMissionRelatedRequestList extends AbstractServlet {

    private static final Logger logger = LoggerFactory.getLogger(GetMissionRelatedRequestList.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        try {
            User user = getReqUser(req);
            List<Request> requestList = getAndCheckInput(req);
            List<Integer> requestIds = RequestUtils.getRequestIds(requestList);

            Query query = new Query(user);
            query.setRequestIds(Utils.list2string(requestIds));
            query.setVersionInfo(getVersionInfo(req));
            query.setFrom("getMissionRelatedRequestList");
            RequestServicePrx requestServicePrx = ServantUtils.getObject(RequestServicePrx.class);
            List<Request> requests = requestServicePrx.getRelatedRequestList(query);

            returnResp(resp, requests);
        } catch (Throwable e) {
            onThrowable(req, resp, e);
        }
    }

    /**
     * 数据格式:
     * 1 查看关联审批的信息 --- [{"requestDkey":},{"requestDkey":}...]
     * (不兼容了)2 筛选出关联了成本分析的审批 --- [{"requestDky":xx, "isRelatedToCostAnalysis":1},{"requestDkey":xx, "isRelatedToCostAnalysis":1}...]
     */
    private List<Request> getAndCheckInput(HttpServletRequest req) throws Throwable {
        String reqBody = getContent(req);
        logger.info("json参数 {}", reqBody);

        List<Request> list = JSONArray.parseArray(reqBody, Request.class);

        if (ListHelper.isNullEmpty(list)) {
            throw new RpcServerLogicalException(1, "无输入参数");
        }

        return list;
    }


    private void returnResp(HttpServletResponse resp, List<Request> list) throws Throwable {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", "0");
        jsonObject.put("res_info", "获得成功");
        jsonObject.put("data", list);

        logger.info(jsonObject.toJSONString());

        printJson(resp, jsonObject.toJSONString());
    }

}
